package ace.tool.generate.code.impl.resolver.scheme;

import ace.tool.generate.code.impl.resolver.scheme.model.bo.*;
import ace.tool.generate.code.impl.resolver.scheme.model.dto.CommonInfo;
import ace.tool.generate.code.impl.resolver.scheme.model.dto.ProjectInfo;
import ace.tool.generate.code.impl.resolver.scheme.resolver.CommonInfoResolver;
import ace.tool.generate.code.impl.resolver.scheme.resolver.ProjectInfoResolver;
import ace.tool.generate.code.impl.resolver.scheme.resolver.api.ApiProjectResolver;
import ace.tool.generate.code.impl.resolver.scheme.resolver.core.CoreProjectResolver;
import ace.tool.generate.code.impl.resolver.scheme.resolver.sdk.SdkProjectResolver;
import ace.tool.generate.code.impl.service.model.input.GenerateInput;

/**
 * @author caspar
 * @date 2023/11/17 10:34
 */
public class GenerateInputResolver {

  private final GenerateInput input;

  public GenerateInputResolver(GenerateInput input) {
    this.input = input;
  }

  public GenerateContext resolve() {
    ModuleProject moduleProject = this.resolveModuleProject();

    return GenerateContext.builder().moduleProject(moduleProject).build();
  }

  private ModuleProject resolveModuleProject() {
    CommonInfo commonInfo = new CommonInfoResolver(input).resolve();

    ProjectInfo moduleProjectInfo = this.getModuleProjectIno();

    ApiProject apiProject = new ApiProjectResolver(input, moduleProjectInfo, commonInfo).resolve();

    SdkProject sdkProject = new SdkProjectResolver(input, moduleProjectInfo, commonInfo).resolve();

    CoreProject coreProject =
        new CoreProjectResolver(input, moduleProjectInfo, commonInfo, apiProject, sdkProject)
            .resolve();

    return ModuleProject.builder()
        .info(moduleProjectInfo)
        .apiProject(apiProject)
        .sdkProject(sdkProject)
        .coreProject(coreProject)
        .build();
  }

  private ProjectInfo getModuleProjectIno() {
    return ProjectInfoResolver.builder()
        .projectNamePrefix(input.getProjectNamePrefix())
        .projectNameSuffix(input.getProjectNameSuffix())
        .projectName(input.getProjectName())
        .projectDisplayName(input.getProjectDisplayName())
        .projectRootDir(input.getProjectRootDir())
        .build()
        .resolve();
  }
}
